 

import Control.*;
import Model.*;

/**
 * UI for Customers
 * @author Stefan
 */
public class CustomersUI {

    private AssociateController assCtr = AssociateController.getInstance();

    /**
     * Start menu
     */
    public void start()
    {
        customersMenu();
    }

    private void customersMenu()
    {
        boolean exit = false;
        while (!exit)
        {
            int choise = writeCustomersMenu();
            if (choise == 1)
            {
                startCreate(CommonUIFunctions.readString("\n write ID: "), CommonUIFunctions.readString("\n write name: "), CommonUIFunctions.readString("\n write address: "), CommonUIFunctions.readString("\n write company: "), 0, CommonUIFunctions.readDouble("\n write Discount: "));
            }
            else if (choise == 2)
            {
                startRead(CommonUIFunctions.readString("\n write Id of the Customer you want to find "));
            }
            else if (choise == 3)
            {
                startUpdate(CommonUIFunctions.readString("\n write the id of Customer update: "));
            }
            else if (choise == 4)
            {
                startDelete(CommonUIFunctions.readString("\n write Id of the Customer you want to delete: "));
            }
            else
            {
                exit = true;
            }
        }
    }

    /**
     * Print Customer menu
     * @return int based on your choise
     */
    private int writeCustomersMenu()
    {
        System.out.println("\f *** Customers menu ***");
        System.out.println(" (1) Create");
        System.out.println(" (2) Read");
        System.out.println(" (3) Update");
        System.out.println(" (4) Delete");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }

    /**
     * Create Menu for Customer
     * @param id
     * @param name
     * @param address
     * @param company
     * @param credit
     * @param discount
     */
    private void startCreate(String id, String name, String address, String company, double credit, double discount)
    {
        int status = assCtr.addCustomer(id, name, address, company, credit, discount);
        if (status == 0)
        {
            //Run method again if Customer ID is in
            startCreate(CommonUIFunctions.readString("Customer ID is already in use, choose another: "), name, address, company, credit, discount);
        }
        else if (status == 1)
        {
            //Run method again if discount is invalid
            startCreate(id, name, address, company, credit, CommonUIFunctions.readDouble("discount must be between 0 and 100 Choose another: "));
        }
    }

    /**
     * Get info about Customer
     * @param id
     */
    private void startRead(String id)
    {
        try
        {
            getInfo(id);
        }
        catch (Exception e)
        {
            System.out.println("There was no user with this ID");
        }
    }

    /**
     * Delete Customer
     * @param id
     */
    private void startDelete(String id)
    {
        if (assCtr.deleteAssociateID(id) == false)
        {
            System.out.println("There was no user with this ID");
        }
    }

    /**
     * Update Customer
     * @param id
     */
    private void startUpdate(String id)
    {
        try
        {
            Customer cus = getInfo(id);
            updateMenu(cus);
        }
        catch (Exception e)
        {
            System.out.println("There was no user with this ID");
        }
    }

    /**
     * Menu for Update Customer
     * @param cus
     */
    private void updateMenu(Customer cus)
    {
        boolean exit = false;
        while (!exit)
        {
            int choise = writeUpdateMenu();
            if (choise == 1)
            {
                String newID = CommonUIFunctions.readString("\n write new ID: ");
                while (assCtr.checkExistingCustomerID(newID))
                {
                    newID = CommonUIFunctions.readString("\n Customer ID is already in use, choose another: ");
                }
                cus.setId(newID);
            }
            else if (choise == 2)
            {
                cus.setName(CommonUIFunctions.readString("\n write new Name: "));
            }
            else if (choise == 3)
            {
                cus.setAddress(CommonUIFunctions.readString("\n write new Address: "));
            }
            else if (choise == 4)
            {
                cus.setCompany(CommonUIFunctions.readString("\n write new Company: "));
            }
            else if (choise == 5)
            {
                cus.setCredit(CommonUIFunctions.readInt("\n write new Credits: "));
            }
            else if (choise == 6)
            {
                cus.setDiscount(CommonUIFunctions.readInt("\n write new Max discount: "));
            }
            else
            {
                exit = true;
            }
        }
    }

    /**
     * Print update menu
     * @return
     */
    private int writeUpdateMenu()
    {
        System.out.println("\f *** UpdateMenu ***");
        System.out.println("\f Choose one to edit");
        System.out.println(" (1) Id ");
        System.out.println(" (2) Name");
        System.out.println(" (3) Address");
        System.out.println(" (4) Company");
        System.out.println(" (5) Credit");
        System.out.println(" (6) Discount");
        System.out.println(" (0) Back");

        return CommonUIFunctions.readInt("\n Make your choise: ");
    }

    /**
     * Get info about Customer
     * @param id
     * @return Customer
     */
    private Customer getInfo(String id)
    {
        Customer cus = assCtr.findCustomerID(id);
        System.out.println("Customer \n" + "Id: " + cus.getId() + "\nName: " + cus.getName() + "\nCompany: " + cus.getCompany() + "\nAddress: " + cus.getAddress() + "\nCredit: " + cus.getCredit() + "\nMax discount: " + cus.getDiscount());
        return cus;
    }
}
